/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdminSetUserRoles.java
 *
 * Created on Aug 15, 2011, 2:13:57 PM
 */
package panels;

import conn.EMIS4Connection;
import dao.LoginDao;
import dao.PersonDao;
import dao.RoleDao;
import dao.SexDao;
import dao.UpdateHistoryDao;
import exec.NotFoundException;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import obj.Login;
import obj.Person;
import obj.Role;
import obj.Sex;
import obj.UpdateHistory;
import util.DashboardUtil;
import util.PersonRequested;

/**
 *
 * @author nknowles
 */
public class AdminSetUserRoles extends javax.swing.JPanel {

  private EMIS4Connection conn = EMIS4Connection.getInstance();
  private PersonRequested pr = PersonRequested.getInstance();
  private UpdateHistory history;
  private UpdateHistoryDao historyDAO;
  private Login login;
  private LoginDao loginDAO;
  private Person person;
  private PersonDao personDAO;
  private RoleDao roleDAO;
  private List<Role> roleList;
  private SexDao sexDAO;
  private List<Sex> sexList;

  /** Creates new form AdminSetUserRoles */
  public AdminSetUserRoles() {
    history = new UpdateHistory();
    historyDAO = new UpdateHistoryDao();
    login = new Login();
    loginDAO = new LoginDao();
    personDAO = new PersonDao();
    roleDAO = new RoleDao();
    sexDAO = new SexDao();

    initComponents();

    fillInformation();
  }

  private void fillInformation() {
    try {
      if (pr.getPerson() == null) {
        DashboardUtil.searchForPatient();
      }
      person = pr.getPerson();
      System.out.println(person.toString());

      // combo box
      conn.connect();
      roleList = roleDAO.loadAll(conn.getConnection());
      sexList = sexDAO.loadAll(conn.getConnection());
      login = loginDAO.getObject(conn.getConnection(), person.getPersonID());

      for (Role item : roleList) {
        roleComboBox.addItem(item.getName());
      }

      // user info
      roleComboBox.setSelectedIndex(person.getRoleID());
      usernameLabel.setText(login.getUsername());
      firstNameLabel.setText(person.getFirstName());
      lastNameLabel.setText(person.getLastName());
      emailLabel.setText(person.getEmail());
      sexLabel.setText(sexList.get(person.getSexID()).getName());

    } catch (NotFoundException ex) {
      Logger.getLogger(AdminSetUserRoles.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
      Logger.getLogger(AdminSetUserRoles.class.getName()).log(Level.SEVERE, null, ex);
    }


  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {
    java.awt.GridBagConstraints gridBagConstraints;

    jLabel1 = new javax.swing.JLabel();
    jPanel2 = new javax.swing.JPanel();
    jLabel4 = new javax.swing.JLabel();
    jLabel11 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel6 = new javax.swing.JLabel();
    jLabel9 = new javax.swing.JLabel();
    usernameLabel = new javax.swing.JLabel();
    firstNameLabel = new javax.swing.JLabel();
    lastNameLabel = new javax.swing.JLabel();
    emailLabel = new javax.swing.JLabel();
    sexLabel = new javax.swing.JLabel();
    roleComboBox = new javax.swing.JComboBox();
    submitButton = new javax.swing.JButton();

    setBackground(new java.awt.Color(220, 220, 210));
    setLayout(new java.awt.GridBagLayout());

    jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
    jLabel1.setText("Set Role");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
    gridBagConstraints.insets = new java.awt.Insets(20, 15, 10, 15);
    add(jLabel1, gridBagConstraints);

    jPanel2.setBackground(new java.awt.Color(220, 220, 210));
    jPanel2.setLayout(new java.awt.GridBagLayout());

    jLabel4.setText("Role:");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 0);
    jPanel2.add(jLabel4, gridBagConstraints);

    jLabel11.setText("User Name:");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
    gridBagConstraints.insets = new java.awt.Insets(8, 0, 0, 0);
    jPanel2.add(jLabel11, gridBagConstraints);

    jLabel2.setText("First Name:"); // NOI18N
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
    gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
    jPanel2.add(jLabel2, gridBagConstraints);

    jLabel3.setText("Last Name:");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
    gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
    jPanel2.add(jLabel3, gridBagConstraints);

    jLabel6.setText("Email:");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 4;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
    gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
    jPanel2.add(jLabel6, gridBagConstraints);

    jLabel9.setText("Sex:");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 5;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
    gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
    jPanel2.add(jLabel9, gridBagConstraints);

    usernameLabel.setText("username");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.insets = new java.awt.Insets(8, 5, 0, 0);
    jPanel2.add(usernameLabel, gridBagConstraints);

    firstNameLabel.setText("first");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.insets = new java.awt.Insets(10, 5, 0, 0);
    jPanel2.add(firstNameLabel, gridBagConstraints);

    lastNameLabel.setText("last");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.insets = new java.awt.Insets(10, 5, 0, 0);
    jPanel2.add(lastNameLabel, gridBagConstraints);

    emailLabel.setText("email");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 4;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.insets = new java.awt.Insets(10, 5, 0, 0);
    jPanel2.add(emailLabel, gridBagConstraints);

    sexLabel.setText("sex");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 5;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.insets = new java.awt.Insets(10, 5, 0, 0);
    jPanel2.add(sexLabel, gridBagConstraints);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
    jPanel2.add(roleComboBox, gridBagConstraints);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
    gridBagConstraints.insets = new java.awt.Insets(5, 25, 0, 15);
    add(jPanel2, gridBagConstraints);

    submitButton.setText("Submit");
    submitButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        submitButtonActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
    gridBagConstraints.weightx = 0.1;
    gridBagConstraints.weighty = 0.1;
    gridBagConstraints.insets = new java.awt.Insets(15, -50, 10, 15);
    add(submitButton, gridBagConstraints);
  }// </editor-fold>//GEN-END:initComponents

private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitButtonActionPerformed
// TODO add your handling code here:
  int oldRole = person.getRoleID();
  int newRole = roleComboBox.getSelectedIndex();
  System.out.printf("Old: %d\tNew: %d", oldRole, newRole);
  if (newRole != oldRole) {
      try {
        // role changed
        person.setRoleID(newRole);
        personDAO.save(conn.getConnection(), person);
        history.setAll("Person", person.getPersonID(), "Role", "Role changed", String.valueOf(oldRole), String.valueOf(newRole));
        historyDAO.create(conn.getConnection(), history);
      } catch (NotFoundException ex) {
        Logger.getLogger(AdminSetUserRoles.class.getName()).log(Level.SEVERE, null, ex);
      } catch (SQLException ex) {
        Logger.getLogger(AdminSetUserRoles.class.getName()).log(Level.SEVERE, null, ex);
      }
  }

}//GEN-LAST:event_submitButtonActionPerformed
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JLabel emailLabel;
  private javax.swing.JLabel firstNameLabel;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel11;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JLabel jLabel6;
  private javax.swing.JLabel jLabel9;
  private javax.swing.JPanel jPanel2;
  private javax.swing.JLabel lastNameLabel;
  private javax.swing.JComboBox roleComboBox;
  private javax.swing.JLabel sexLabel;
  private javax.swing.JButton submitButton;
  private javax.swing.JLabel usernameLabel;
  // End of variables declaration//GEN-END:variables
}
